package com.asia5b.a5mall.member.activity.comment

import android.app.Activity
import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.member.R
import com.asia5b.a5mall.member.adapter.PostCommentAdapter
import com.asia5b.a5mall.member.databinding.ActivityPostcommentBinding
import com.asia5b.a5mall.middleware.base.SimpleActivity
import com.asia5b.a5mall.middleware.event.PostCommentEvent
import com.asia5b.a5mall.middleware.logic.viewmodel.member.PostCommentActivityVM
import com.asia5b.a5mall.middleware.model.bean.PostCommentBean
import com.asia5b.a5mall.middleware.model.bean.PostGoodsBean
import com.asia5b.a5mall.middleware.model.order.OrderDetailModel
import com.asia5b.a5mall.middleware.model.shop.AbsGoodsModel
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.utils.*
import com.asia5b.a5mall.middleware.widget.CancelSureViewModel
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.entity.LocalMedia
import kotlinx.android.synthetic.main.activity_postcomment.*

/**
 * Created by zhangyuncai on 2019/12/21.
 * @Note   评论编辑页
 */
@Route(path = ActCons.PostCommentActivity)
class PostCommentActivity : SimpleActivity<PostCommentActivityVM, ActivityPostcommentBinding>() {
    override val viewModelClazz: Class<PostCommentActivityVM>
        get() = PostCommentActivityVM::class.java

    override fun getDataBinding(): ActivityPostcommentBinding {
        ARouter.getInstance().inject(this)
//        orderSn=intent.getStringExtra("orderSn")
        if (intent.hasExtra("data")) {
            orderDetailModel = intent.getSerializableExtra("data") as OrderDetailModel
        }
        return ActivityPostcommentBinding.inflate(layoutInflater)
    }

    override var sIsDark = true

    @Autowired(name = "orderSn", required = true)
    @JvmField
    var orderSn: String = ""
    var orderDetailModel: OrderDetailModel? = null

    val mRecyclerView by lazy { rv_postcomment }
    val mDatas = arrayListOf<AbsGoodsModel>()
    val mAdapter by lazy { PostCommentAdapter(mDatas) }


    override fun initView() {
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(mActivity)!!
            adapter = mAdapter
        }
        orderDetailModel?.order_sku_list?.run {
            mDatas.clear()
            mDatas.addAll(this)
            mAdapter.notifyDataSetChanged()
        }
        orderDetailModel?.run {
            mViewModel.getShop(this.seller_id)
        }
    }

    fun commit() {
        val goods = ArrayList<PostGoodsBean>()
        val goodsList = mAdapter.data
        goodsList.forEach {
            val item = PostGoodsBean()
            item.content = it.commentContent.get()!!
            if (it.goodsStar < 3) {
                item.grade = "bad"
            } else if (it.goodsStar == 3 || it.goodsStar == 4) {
                item.grade = "neutral"
            } else {
                item.grade = "good"
            }
            it.selectionMedia?.forEach {
                item.waitUploadImages.add(it.compressPath)
            }
            item.sku_id = it.absGoodsSkuId
            dddLog("size:${it.selectionMedia?.size}")
            goods.add(item)
        }
        val result = PostCommentBean()
        result.delivery_score = orderDetailModel!!.logisticsStar
        result.description_score = orderDetailModel!!.desStar
        result.service_score = orderDetailModel!!.serviceStar
        result.order_sn = orderDetailModel!!.sn
        result.comments = goods

        if (false) {
            return
        }
        mViewModel.commit(result)
    }

    override fun initEvent() {
        head_postcomment.setRightClickListener {
            commit()
        }
        //描述相符
        rb_shop_descstar.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
            orderDetailModel!!.desStar = rating.toInt()
            tv_shop_descstar.text = String.format("%.1f", rating)
        }
        //物流相符
        rb_shop_expressstar.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
            orderDetailModel!!.logisticsStar = rating.toInt()
            tv_shop_expressstar.text = String.format("%.1f", rating)
        }
        //服务相符
        rb_shop_serverstar.setOnRatingBarChangeListener { ratingBar, rating, fromUser ->
            orderDetailModel!!.serviceStar = rating.toInt()
            tv_shop_serverstar.text = String.format("%.1f", rating)
        }
    }

   override fun startObserver()
    {
        mViewModel.commitResultLiveData.observe(this, Observer {
            commitResult()
        })
        mViewModel.getShopResultLiveData.observe(this, Observer {
            getShopResult(it)
        })
    }

    override fun initData() {

    }

    fun commitResult() {
        showMessage(getString(R.string.发表成功))
        getEventBus().post(PostCommentEvent())
        pop()
    }

    fun getShopResult(shopDetailModel: AbsShopModel) {
        shopDetailModel.run {
            //店铺图片
            WImageLoader.displayImage(absShopLogo, iv_postcomment_shoplogo, R.drawable.img_default)
            //店铺名称
            iv_postcomment_shopname.text = absShopName
            //店铺评分
            rb_postcomment_shop_star.rating = absShopCredit.toFloat()
            //店铺评分值
            iv_postcomment_starvalue.text = String.format("%.1f", absShopCredit)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK)
            return
        var uploadPosition = requestCode
        // 图片选择
        val selectList = PictureSelector.obtainMultipleResult(data)!!
        val selectionMedia = arrayListOf<LocalMedia>()
        selectList.forEach {
            selectionMedia.add(it)
        }
        if (mDatas.size > uploadPosition) {
            mDatas.get(uploadPosition).selectionMedia = selectionMedia
            mAdapter.notifyItemChanged(uploadPosition)
        }
    }

    fun showNoticeDialog() {
        CancelSureViewModel(mActivity).apply {
            contentStringRes = R.string.当前修改尚未提交_是否离开
            leftColorInt = R.color.colorPrimaryDark
            rihghtColorInt = R.color.common_text_gray
            rightClick = {
                pop()
            }
        }.show()
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        showNoticeDialog()
    }

    override fun backClick(v: View) {
//        super.backClick(v)
        showNoticeDialog()
    }
}